package com.china08.yxyapi.api.manage.tec.teaching.mistakes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.entity.mg.mistakes.TeaMistakesQues;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.es.ElasticYxyYxyQuesResourceRespository;
import com.china08.yxyapi.repository.mg.mistakes.TeaMistakesQuesRepository;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

/**
 * 教师视频讲解错题查看
 * 
 * @author Administrator
 *
 */
@CrossOrigin
@RestController
public class TeaMistakesQuesStatusSchController extends YxyApiBaseController {

    @Autowired
    TeaMistakesQuesRepository teaMistakesQuesRepository;

    @Autowired
    ElasticYxyYxyQuesResourceRespository elasticYxyYxyQuesResourceRespository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/mistakes/mistakesquesstatus/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel model)
            throws JsonParseException, JsonMappingException, IOException {
        RespModel respModel = new RespModel();
        TeaMistakesQues tmq = teaMistakesQuesRepository.findOne(model.id);
        Assert.notNull(tmq, "未找到该错题");

        ElasticYxyYxyQuesResource eyyqr = elasticYxyYxyQuesResourceRespository.findById(tmq.getQuesId());
        Assert.notNull(eyyqr, "未找到该错题详细信息");

        respModel.quesId = tmq.getQuesId();
        respModel.ques = eyyqr.getQuesBody();
        respModel.sources = tmq.getSources();
        respModel.stage = tmq.getStage();
        respModel.subject = tmq.getSubject();
        List<KnowId> knowLst = new ArrayList<TeaMistakesQuesStatusSchController.KnowId>();
        for (int i = 0; i < tmq.getKnowIdLst().size(); i++) {
            KnowId knowId = new KnowId();
            knowId.knowId = tmq.getKnowIdLst().get(i);
            knowId.knowName = tmq.getKnowNameLst().get(i);
            knowLst.add(knowId);
        }
        respModel.knowId = knowLst;
        respModel.mistakesNumber = tmq.getMistakesNumber();
        return respModel;
    }

    /**
     * 请求参数
     * 
     */
    protected static class ReqModel extends YxyApiPageReqModel {
        // id
        public String id;
    }

    /**
     * 响应参数
     * 
     */
    protected static class RespModel extends YxyApiRespModel {
        // 试题:ID
        public String quesId;

        // 试题:对象
        public Object ques;

        // 错题来源,00--作业系统 01--测评系统
        public List<String> sources;

        public List<KnowId> knowId;

        // 错误人数
        public Integer mistakesNumber;

        // 学段
        public String stage;

        // 学科
        public String subject;
    }

    /**
     * 知识点
     * 
     * @author Administrator
     *
     */
    protected static class KnowId {
        // 知识点:ID
        public Integer knowId;

        // 知识点:Name
        public String knowName;
    }
}
